
package GameEngine;

/**
 * This class was created to stop the time.
 * It's important because every player have a limit of time for this game
 */

public class Time{

    private Status actStatus = new Status(0);

    /**
     * startTime 
     * endTime
     * volume    , consists the left time
     * volumeMax , consists the max time
     * ID
     */
	private long startTime;
	private long endTime;
	private double volume;
	private double volumeMax;
    private static int ID;

	public Time(){
		super();
	}


    /**
     * Constructor to create a object of the class Time
     * @param value this parameter sets the upper bound of time to play
     */
	public Time(double value){
			startTime = 0;
			endTime = 0;
			volume = 0.0;
			volumeMax = value;
            ID ++;

	}


    /**
     * Setter-method to set the start point
     */
	public void start(){
		this.startTime = System.currentTimeMillis();
	}


    /**
     * Setter-method to set the end point, calculates the difference of start point and end point
     * and add this value to volume.
     * At last it checks the balance
     */
	public void end(){

		this.endTime = System.currentTimeMillis();
		double diff =  (this.endTime - this.startTime)/ 1000.0; //calc MilliSec to Sec
		this.volume += diff;
		checkTime();		//checks the balance

	}


    /**
     *Getter-method to get the balance of the time
     * @return returns the time you have for the rest of the play
     */
	public double getVolume(){
		return (this.volumeMax - this.volume);
	}


    /**
     * This Method checks the balance
     * If one player have no time anymore the game is over
     * and the opponent wins this session
     * The Program will terminate itself
     */
	private void checkTime(){

		if(this.volumeMax - this.volume <= 0.0){
			System.out.println("Time's up!!!");

            //ID = 1 represents the Black-player
            //ID = 2 represents the Red-player
            if(ID == 1 ){
                System.out.println("Red wins cause time is left for Black");
                actStatus.value(2);
            }
            if(ID == 2){
                System.out.println("Black wins cause time is left for Red");
                actStatus.value(1);

            }
			System.out.println( getVolume() );
			System.exit(1);	
		}
	}
}